package com.bkgy.modular.calsidata.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baosight.hdsdk.domain.data.HDDataConnection;
import com.baosight.hdsdk.domain.data.HDRecord;
import com.baosight.hdsdk.exception.HDSdkException;
import com.bkgy.modular.calsidata.dao.C622TPdoListDao;
import com.bkgy.modular.calsidata.dao.StatisticalData2130Mapper;
import com.bkgy.modular.calsidata.dao.TbDsjC622FlatPassTimeMapper;
import com.bkgy.modular.calsidata.dao.TbFaltMapper;
import com.bkgy.modular.calsidata.entity.C622TPdoList;
import com.bkgy.modular.calsidata.entity.TbDsjC622FlatPassTime;
import com.bkgy.modular.calsidata.entity.TbFalt;
import com.bkgy.controller.tbbxrecord.entity.TbBxFileSave;
import com.bkgy.modular.tbdsjdatacollect.dao.TbDsjDataCollectDao;
import com.bkgy.modular.tbdsjdatacollect.entity.TbDsjDataCollect;
import com.bkgy.util.PdoUtils;
import com.bkgy.util.SDKUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ZhuHongKai
 * @version 1.0
 * @ClassName StatisticalData2130Service1
 * @date 2022/12/12 9:04
 */
@Service
@Slf4j
public class StatisticalData2130Service {

    @Resource
    private TbDsjC622FlatPassTimeMapper tbDsjC622FlatPassTimeMapper;

    @Resource
    private StatisticalData2130Mapper statisticalData2130Mapper;

    @Resource
    private SDKUtils sdkUtils;

    @Resource
    private C622TPdoListDao c622TPdoListDao;

    @Resource
    private TbDsjDataCollectDao tbDsjDataCollectDao;

    @Resource
    private TbFaltMapper tbFaltMapper;

    @Resource
    private RestTemplate restTemplate;

    @Autowired
    private PdoUtils pdoUtils;

   // @Scheduled(cron = "${cronTime7}")//定时在112全打开，113，115全部关闭
    public void statisticalData() {
        try {
            log.error("StatisticalData2130Service+statisticalData");
            // 构建查询条件通过passTime表最新时间降序排列,取出第一条数据,按照时间从pdo_List表查询最新大于该时间的未处理的生产卷号
            QueryWrapper<TbDsjC622FlatPassTime> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().orderByDesc(TbDsjC622FlatPassTime::getId).last(" FETCH FIRST 1 ROWS ONLY");
            TbDsjC622FlatPassTime tbDsjC622FlatPassTime = tbDsjC622FlatPassTimeMapper.selectOne(queryWrapper);

            /*LambdaQueryWrapper<StatisticalData2130> queryWrapper1 = new LambdaQueryWrapper<>();
            List<StatisticalData2130> statisticalData2130List = statisticalData2130Mapper.selectList(queryWrapper1);*/

            LambdaQueryWrapper<C622TPdoList> queryWrapper3 = new LambdaQueryWrapper<>();
            queryWrapper3.gt(StringUtils.hasText(tbDsjC622FlatPassTime.getProcess_end_time().toString()), C622TPdoList::getEndtime, tbDsjC622FlatPassTime.getProcess_end_time()).isNotNull(C622TPdoList::getEndtime);
            // queryWrapper1.gt(StatisticalData2130::getProcess_end_time,"2023-06-05 14:33:35");

            List<C622TPdoList> c622TPdoLists = c622TPdoListDao.selectList(queryWrapper3);

            for (C622TPdoList item : c622TPdoLists) {
                TbBxFileSave tbBxFileSave = new TbBxFileSave();
                tbBxFileSave.setMessage("最复杂");
                pdoUtils.calPdo622(item.getExtid(), tbBxFileSave);
            }
            // 创建宝信实时数据库连接
            HDDataConnection connect = sdkUtils.getConnect();
            while (!connect.isConnected()) {
                connect = sdkUtils.getConnect();
            }
            // 获取C622_N10181_9信号点
            // 根据C622_N10181_12查询出tagId
            Integer id = sdkUtils.findTagId("C622_N10181_12", connect.getTagManager());

            C622TPdoList c622TPdoList = null;
            // 循环读取获取到的还未处理的最新卷号时间
            // 根据pdo_List的endTime截取信号点 从0变1的时刻点
            for (C622TPdoList item : c622TPdoLists) {
                LambdaQueryWrapper<C622TPdoList> queryWrapper2 = new LambdaQueryWrapper<>();
                queryWrapper2.eq(C622TPdoList::getExtid, item.getExtid());
                c622TPdoList = c622TPdoListDao.selectOne(queryWrapper2);
                HDRecord hdRecord = null;
                LocalDateTime pdoDataDate = c622TPdoList.getEndtime().toLocalDateTime();
                LocalDateTime startTime = item.getStarttime().toLocalDateTime();

                // 判断startTime和localDateTime的时间范围小于30分钟
                if (Duration.between(startTime, pdoDataDate).toMinutes() < 30) {
                    //限制最多查询30分钟
                    LocalDateTime stuTime = pdoDataDate.minusMinutes(30);
                    // 每次循环5分钟
                    LocalDateTime pdoDataDate2 = pdoDataDate.minusMinutes(5);
                    int a = 1;
                    while (a == 1) {
                        List<HDRecord> hdRecords = sdkUtils.findMsgByIdAndTime(id, connect.getDataProvider(), pdoDataDate2, pdoDataDate);
                        for (int i = 0; i < hdRecords.size(); i++) {
                            // 信号下降沿,表示带钢焊缝过渡完成
                            if (hdRecords.get(i).getValueStr().equals("1.0")) {
                                if (i == 0) {
                                    hdRecord = hdRecords.get(i);
                                } else {
                                    hdRecord = hdRecords.get(i - 1);
                                }
                                a = 0;
                                break;
                            }
                        }
                        pdoDataDate = pdoDataDate.minusMinutes(5);
                        pdoDataDate2 = pdoDataDate2.minusMinutes(5);
                        if (pdoDataDate2.isEqual(stuTime) || a == 0) {
                            a = 0;
                            break;
                        }
                    }


                    //获取PassTime表上条数据的结束时间(Pass_device_end_time),用于后续给本条的开始时间赋值
                    TbDsjC622FlatPassTime latestData = null;
                    LambdaQueryWrapper<TbDsjC622FlatPassTime> wrapper = new LambdaQueryWrapper<>();
                    // wrapper.orderByDesc(TbDsjC622FlatPassTime::getId).last(" FETCH FIRST 1 ROWS ONLY");
                    wrapper.orderByDesc(TbDsjC622FlatPassTime::getId).lt(TbDsjC622FlatPassTime::getProcess_end_time, item.getEndtime()).last(" FETCH FIRST 1 ROWS ONLY");
                    // wrapper.orderByDesc(TbDsjC622FlatPassTime::getId).eq(TbDsjC622FlatPassTime::getCoil_id,"C236004472");
                    //查询上条数据
                    latestData = tbDsjC622FlatPassTimeMapper.selectOne(wrapper);


                    LocalDateTime localDateTime2 = null; // 宝信实时数据库获取时间
                    if (!ObjectUtils.isEmpty(hdRecord)) {
                        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS");
                        localDateTime2 = LocalDateTime.parse(hdRecord.getTimeStampStr(), pattern);
                    }
                    if (c622TPdoList != null) {
                        // 判断(宝信实时数据库获取时间)localDataTime2不为null
                        if (localDateTime2 != null) {
                            // 判断latestData(上条数据时间)是否为空不为空返回false(取反),并且latestData中的getPass_device_end_time不等于null,
                            // 并且Timestamp.valueOf(localDateTime2)(宝信实时数据库获取的时时间) != null满足该三个条件判断为true
                            if (!ObjectUtils.isEmpty(latestData) && latestData.getPass_device_end_time() != null && Timestamp.valueOf(localDateTime2) != null) {
                                // 信号截取有问题的时候会导致上条数据的结束时间大于等于本条结束时间的时候,用pdo_List的结束时间(Endtime)进行赋值生产结束时间(Process_end_time)和当前卷的尾部焊缝通过测厚仪时间(Pass_device_end_time)
                                if (latestData.getPass_device_end_time().getTime() >= Timestamp.valueOf(localDateTime2).getTime()) {
                                    log.error("1上个卷号" + latestData.getCoil_id() + "时间:" + latestData.getPass_device_end_time() + "和当前卷号" + item.getExtid() + "时间:" + localDateTime2);
                                    TbDsjC622FlatPassTime tbDsjC622FlatPassTime1 = new TbDsjC622FlatPassTime();
                                    tbDsjC622FlatPassTime1.setCoil_id(item.getExtid());
                                    tbDsjC622FlatPassTime1.setHot_coil_id(item.getEntid());
                                    tbDsjC622FlatPassTime1.setPass_device_end_time(c622TPdoList.getEndtime());
                                    tbDsjC622FlatPassTime1.setProcess_end_time(c622TPdoList.getEndtime());
                                    tbDsjC622FlatPassTime1.setPass_device_start_time(latestData.getPass_device_end_time());
                                    tbDsjC622FlatPassTime1.setFlag(1);
                                    //插入最新数据
                                    QueryWrapper<TbDsjC622FlatPassTime> wrapper3 = new QueryWrapper<>();
                                    wrapper3.eq("coil_id", tbDsjC622FlatPassTime1.getCoil_id());
                                    Long count = tbDsjC622FlatPassTimeMapper.selectCount(wrapper3);
                                    if (count <= 0) {
                                        tbDsjC622FlatPassTimeMapper.insert(tbDsjC622FlatPassTime1);
                                    }
                                } else {
                                    // 信号截取没有问题的时候,用pdo_List的结束时间(Endtime)进行赋值生产结束时间(Process_end_time)和当前卷的尾部焊缝通过测厚仪时间(Pass_device_end_time)
                                    TbDsjC622FlatPassTime data = new TbDsjC622FlatPassTime();
                                    // 给本卷的生产结束时间赋值
                                    data.setCoil_id(item.getExtid())
                                            .setHot_coil_id(item.getEntid())
                                            .setProcess_end_time(c622TPdoList.getEndtime());
                                    // 给本卷的头部焊缝通过测厚仪时间赋值
                                    if (!ObjectUtils.isEmpty(latestData)) {
                                        data.setPass_device_start_time(latestData.getPass_device_end_time());
                                    }
                                    // 判断信号点数据为不为空,直接将焊缝尾部通过测厚仪时间赋值为宝信实时数据库时间
                                    if (!ObjectUtils.isEmpty(hdRecord)) {
                                        // 创建DateTimeFormatter对象,传入格式化字符串
                                        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS");
                                        // 将hdRecord.getTimeStampStr()转成按照指定格式
                                        LocalDateTime parse = LocalDateTime.parse(hdRecord.getTimeStampStr(), pattern);

                                        // TODO 六月5日修改 PASS_DEVICE_START_TIME&PASS_DEVICE_END_TIME间隔时间短的问题
                                        if (Duration.between(data.getPass_device_start_time().toLocalDateTime(), parse)
                                                .toMinutes() > 1) {
                                            // 判断卷号结尾是否为C,为C的话Pass_device_end_time值使用process_end_time
                                            data.setPass_device_end_time(
                                                    item.getExtid().endsWith("C") ?
                                                            c622TPdoList.getEndtime() : Timestamp.valueOf(parse));
                                        } else {
                                            log.error("2上个卷号" + latestData.getCoil_id() + "和当前卷号" + item.getExtid());
                                            data.setPass_device_end_time(c622TPdoList.getEndtime());
                                        }

                                        /* if ((data.getPass_device_start_time().toLocalDateTime()).equals(parse)) {
                                            log.error("2上个卷号" + latestData.getCoil_id()+ "和当前卷号" + item.getExtid());
                                            data.setPass_device_end_time(item.getProcess_end_time());
                                        } else {
                                            data.setPass_device_end_time(Timestamp.valueOf(parse));
                                        }*/
                                        data.setFlag(1);
                                    }

                                    // 插入最新数据
                                    QueryWrapper<TbDsjC622FlatPassTime> wrapper1 = new QueryWrapper<>();
                                    wrapper1.eq("coil_id", data.getCoil_id());
                                    Long count = tbDsjC622FlatPassTimeMapper.selectCount(wrapper1);
                                    if (count <= 0) {
                                        tbDsjC622FlatPassTimeMapper.insert(data);
                                    }
                                }
                            } else {
                                // if (!ObjectUtils.isEmpty(latestData) && latestData.getPass_device_end_time() != null && Timestamp.valueOf(localDateTime2) != null) 等于 false
                                // 创建TbDsjC622FlatPassTime对象
                                TbDsjC622FlatPassTime data = new TbDsjC622FlatPassTime();
                                log.error("3上个卷号" + latestData.getCoil_id() + "和当前卷号" + item.getExtid());
                                data.setCoil_id(item.getExtid())
                                        .setHot_coil_id(item.getEntid())
                                        .setProcess_end_time(c622TPdoList.getEndtime())
                                        .setPass_device_start_time(latestData.getPass_device_end_time())
                                        .setPass_device_end_time(c622TPdoList.getEndtime())
                                        .setFlag(1);
                                //插入最新数据
                                QueryWrapper<TbDsjC622FlatPassTime> wrapper2 = new QueryWrapper<>();
                                wrapper2.eq("coil_id", data.getCoil_id());
                                Long count = tbDsjC622FlatPassTimeMapper.selectCount(wrapper2);
                                if (count <= 0) {
                                    tbDsjC622FlatPassTimeMapper.insert(data);
                                }
                            }
                        } else {
                            // 判断(宝信实时数据库获取时间)localDataTime2为null
                            // 创建TbDsjC622FlatPassTime对象
                            TbDsjC622FlatPassTime data = new TbDsjC622FlatPassTime();
                            log.error("4上个卷号" + latestData.getCoil_id() + "和当前卷号" + item.getExtid());
                            data.setCoil_id(item.getExtid())
                                    .setHot_coil_id(item.getEntid())
                                    .setProcess_end_time(c622TPdoList.getEndtime())
                                    .setPass_device_start_time(latestData.getPass_device_end_time())
                                    .setPass_device_end_time(c622TPdoList.getEndtime())
                                    .setFlag(1);
                            //插入最新数据
                            QueryWrapper<TbDsjC622FlatPassTime> wrapper3 = new QueryWrapper<>();
                            wrapper3.eq("coil_id", data.getCoil_id());
                            Long count = tbDsjC622FlatPassTimeMapper.selectCount(wrapper3);
                            if (count <= 0) {
                                tbDsjC622FlatPassTimeMapper.insert(data);
                            }
                        }
                    }
                } else {
                    // 判断startTime和localDateTime的时间范围大于等于30分钟,此时不查询实时数据库,直接使用Pdo_List的结束时间赋值,由于查询实时数据库时间需要一个上限,否则查询会不受限制,会导致程序异常
                    //上条数据
                    TbDsjC622FlatPassTime latestData = null;
                    //存数据库
                    QueryWrapper<TbDsjC622FlatPassTime> wrapper = new QueryWrapper<>();
                    wrapper.lambda().orderByDesc(TbDsjC622FlatPassTime::getId).last(" FETCH FIRST 1 ROWS ONLY");
                    //查询上条数据
                    latestData = tbDsjC622FlatPassTimeMapper.selectOne(wrapper);

                    LambdaQueryWrapper<C622TPdoList> wq = new LambdaQueryWrapper<>();
                    wq.eq(C622TPdoList::getExtid, item.getExtid());
                    c622TPdoList = c622TPdoListDao.selectOne(wq);
                    if (c622TPdoList != null) {
                        TbDsjC622FlatPassTime TbDsjC622FlatPassTime = new TbDsjC622FlatPassTime();
                        TbDsjC622FlatPassTime.setCoil_id(item.getExtid());
                        TbDsjC622FlatPassTime.setHot_coil_id(item.getEntid());
                        TbDsjC622FlatPassTime.setProcess_end_time(c622TPdoList.getEndtime());
                        log.error("5上个卷号" + latestData.getCoil_id() + "和当前卷号" + item.getExtid());
                        TbDsjC622FlatPassTime.setPass_device_end_time(c622TPdoList.getEndtime());
                        if (latestData == null) {
                            TbDsjC622FlatPassTime.setPass_device_start_time(null);
                        } else {
                            TbDsjC622FlatPassTime.setPass_device_start_time(latestData.getPass_device_end_time());
                        }
                        TbDsjC622FlatPassTime.setFlag(1);
                        //插入最新数据
                        QueryWrapper<TbDsjC622FlatPassTime> wrapper3 = new QueryWrapper<>();
                        wrapper3.eq("coil_id", TbDsjC622FlatPassTime.getCoil_id());
                        Long count = tbDsjC622FlatPassTimeMapper.selectCount(wrapper3);
                        if (count <= 0) {
                            tbDsjC622FlatPassTimeMapper.insert(TbDsjC622FlatPassTime);
                        }
                    }
                }

            }
            try {
                connect.dispose();
            } catch (HDSdkException e) {
                log.error("StatisticalData2130Service+statisticalData+connect释放失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 //   @Scheduled(cron = "${cronTime8}")//定时在112全打开，113，115全部关闭
    public void saveStatiscalData() {
        // 查找需要查询的sdk变量名、别名
        log.error("StatisticalData2130Service+saveStatiscalData");
        LambdaQueryWrapper<TbDsjDataCollect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TbDsjDataCollect::getOut_data_source, "7.0");
        List<TbDsjDataCollect> list1 = tbDsjDataCollectDao.selectList(queryWrapper);

        LambdaQueryWrapper<TbDsjDataCollect> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.and(item -> item.eq(TbDsjDataCollect::getOut_data_source, "3.0").eq(TbDsjDataCollect::getProduction_line, "C622"));
        List<TbDsjDataCollect> tbDsjDataCollectList = tbDsjDataCollectDao.selectList(queryWrapper2);

        // 查找还未处理的卷
        LambdaQueryWrapper<TbDsjC622FlatPassTime> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(TbDsjC622FlatPassTime::getFlag, 1).orderByAsc(TbDsjC622FlatPassTime::getPass_device_end_time);
        List<TbDsjC622FlatPassTime> list2 = tbDsjC622FlatPassTimeMapper.selectList(queryWrapper1);
        ArrayList<TbDsjC622FlatPassTime> list3 = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list2)) {
            list3.add(list2.get(0));
        }
        TbFalt tbFalt = new TbFalt();
        HashMap<String, Object> falt = new HashMap<>();
        long len = 0L;
        HDDataConnection connect = sdkUtils.getConnect();
        while (!connect.isConnected()) {
            connect = sdkUtils.getConnect();
        }
        // 循环获取时间范围
        for (TbDsjC622FlatPassTime item : list3) {
            if (!String.valueOf(item.getPass_device_start_time()).equals("null") && !String.valueOf(item.getPass_device_end_time()).equals("null") && StringUtils.hasText(String.valueOf(item.getPass_device_start_time())) && StringUtils.hasText(String.valueOf(item.getPass_device_end_time()))) {
                LocalDateTime start_time = item.getPass_device_start_time().toLocalDateTime();
                LocalDateTime end_time = item.getPass_device_end_time().toLocalDateTime();

                if (start_time.isBefore(end_time)) {
                    // 代入卷号
                    tbFalt.setExtid(item.getCoil_id());

                    // List<Double> length = new ArrayList<>();

                    List<Double> thick_lmi = new ArrayList<>();
                    List<HDRecord> list_length = sdkUtils.findMsgByIdAndTime(
                            sdkUtils.findTagId("C622_N11D01", connect.getTagManager()),
                            connect.getDataProvider(),
                            start_time, end_time
                    );
                    Collections.reverse(list_length);
                    // 积分值
                    Double integral = 0.0;
                    List<Double> length = new ArrayList<>();
                    for (HDRecord hdRecord : list_length) {
                        String valueStr = hdRecord.getValueStr();
                        integral += Double.parseDouble(valueStr) * 20 / 60 / 1000;
                        length.add(integral);
                    }

                    // 获取长度
                /*length = list_length.stream().filter(x -> StringUtils.hasText(x.getValueStr())).map(x -> Double.parseDouble(x.getValueStr()))
                        .sorted().collect(Collectors.toList());*/

                    //long length = Math.round(integral);

                    List<String> stringList = new ArrayList<>();
                    for (TbDsjDataCollect item2 : list1) {
                        stringList.add(item2.getL1_variable_name());
                    }
                    List<ImmutablePair<Integer, Integer>> tagIDsByTagNamesList = sdkUtils.findTagIDsByTagNames(stringList, connect.getTagManager());
                    int i2 = 0;
                    // 根据变量名循环获取tagid
                    for (TbDsjDataCollect entity : list1) {
                        List<HDRecord> list_length2 = sdkUtils.findMsgByIdAndTime(tagIDsByTagNamesList.get(i2).getLeft(), connect.getDataProvider(), start_time, end_time);
                        Collections.reverse(list_length2);

                        // 获取要计算的值的集合 null
                        List<Double> thick = list_length2.stream().filter(x -> StringUtils.hasText(x.getValueStr()))
                                .map(x -> Double.parseDouble(x.getValueStr())).collect(Collectors.toList());
                        // 根据变量名、时间范围,计算转换出需要的值集合
                        thick_lmi = changeData(length, thick);
                        // 获取别名,并存储转换后的对应数据
                        falt.put(entity.getL1_variable_alias(), thick_lmi);
                        len = thick_lmi.size();
                        i2++;
                    }
                }

                // 改变标识符
                item.setFlag(2);
                tbDsjC622FlatPassTimeMapper.updateById(item);
                tbFalt.setEdge(falt);
                tbFalt.setLength(len);
                Map<String, Object> map = new HashMap<>();
                List<String> tbDsjDataCollectLists = new ArrayList<>();
                for (TbDsjDataCollect tbDsjDataCollect : tbDsjDataCollectList) {
                    tbDsjDataCollectLists.add(tbDsjDataCollect.getL1_variable_name());
                }
                int i3 = 0;
                for (TbDsjDataCollect tbDsjDataCollect : tbDsjDataCollectList) {
                    // 根据L1_variable_name去sdk查询tagID
                    List<ImmutablePair<Integer, Integer>> tagIDsByTagNames = sdkUtils.findTagIDsByTagNames(tbDsjDataCollectLists, connect.getTagManager());

                    // 根据tabID和tbDsjC622FlatPassTime表中的开始结束时间去查询sdk没条数据
                    List<HDRecord> hdRecordList = sdkUtils.findMsgByIdAndTime(tagIDsByTagNames.get(i3).getLeft(), connect.getDataProvider(), start_time, end_time);

                    map.put(tbDsjDataCollect.getL1_variable_alias(), hdRecordList.get(0).getValueStr());
                    tbFalt.setEdgeList(map);
                    i3++;
                }
                tbFalt.setCreatetime(new Date());
                tbFaltMapper.save(tbFalt);
                sendmessage(tbFalt);
            } else {
                // 改变标识符
                item.setFlag(2);
                tbDsjC622FlatPassTimeMapper.updateById(item);
            }
        }
        try {
            connect.dispose();
        } catch (HDSdkException e) {
            log.error("StatisticalData2130Service+saveStatiscalData+connect释放失败");
        }
    }

    private void sendmessage(TbFalt tbFalt) {
        /*JSONObject jsonObject = new JSONObject();
        jsonObject.put("volumeNo",tbFalt.getExtid());
        jsonObject.put("line","C2130");
        String jsonString = jsonObject.toJSONString();
        restTemplate.postForObject("http://170.0.200.112:1234/send_msg_to_models", jsonString, String.class);*/

        ///先查表，满足flag=2,并且满足flag1=0,发送消息，并且flag1改为1
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("volumeNo", tbFalt.getExtid());
        jsonObject.put("line", "C622");
        String jsonString = jsonObject.toJSONString();
        LambdaQueryWrapper<TbDsjC622FlatPassTime> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(TbDsjC622FlatPassTime::getCoil_id, tbFalt.getExtid());
        queryWrapper1.eq(TbDsjC622FlatPassTime::getFlag, 2);
        queryWrapper1.eq(TbDsjC622FlatPassTime::getFlag1, 0);
        TbDsjC622FlatPassTime tbDsjC622FlatPassTime = tbDsjC622FlatPassTimeMapper.selectOne(queryWrapper1);
        if (tbDsjC622FlatPassTime != null) {
            restTemplate.postForObject("http://170.0.200.112:1234/send_msg_to_models", jsonString, String.class);
            tbDsjC622FlatPassTime.setFlag1(1);
            tbDsjC622FlatPassTimeMapper.updateById(tbDsjC622FlatPassTime);
        }
    }

    private List<Double> changeData(List<Double> length, List<Double> thick) {
        List<Double> thick_lmi = new ArrayList<>();
        int M = thick.size();
        double N = length.stream().mapToDouble(x -> x).max().orElse(0.0D);//最大值
//        double N = length.get(0);//最大值
//        log.info("N是:  -----"+N);
//        log.info("M是:  -----"+M);
//        log.info("length.size是:  -----"+length.size());
        //补充thick长度和lenth至一致
        if (M < length.size()) {
            for (int j = thick.size(); j < length.size(); j++) {
                if (j > 0) {
                    thick.add(j, thick.get(j - 1));
                }
            }
        }
        int index = 0;
        for (int n = 0; n < N; n++) {
            for (int m = index; m < M; m++) {
                if (m < length.size() && length.get(m) > n + 1 && index <= m - 1) {
//                    log.info("n是:  -----"+n);
//                    log.info("length.get(m)是:  -----"+length.get(m));
                    thick_lmi.add(n, recursion(thick, index, m - 1));
                    index = m;
                    break;
                }
            }
        }
        return thick_lmi;
    }

    private static Double recursion(List<Double> list, int L, int R) {
        if (L == R || L > R) {
            return list.get(L);
        }
        List<Double> listx = new ArrayList<>();
        for (int i = L; i < R; i++) {
            listx.add(list.get(i));
        }
        return listx.stream().mapToDouble(x -> x).max().orElse(0.0D);
    }
}
